package com.le.medium.class8;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 最长无重复子串
 */
public class Problem04_LongestNoRepeatSubstring {

    public static int getLength(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        char[] chs = s.toCharArray();

        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < chs.length; i++) {
            map.put(chs[i], -1);
        }
        int len = 0;
        int pre = -1;
        int cur = 0;
        for (int i = 0; i < chs.length; i++) {
            pre = Math.max(pre, map.get(chs[i]));
            cur = i - pre;
            len = Math.max(len, cur);
            map.put(chs[i], i);
        }
        return len;
    }

    @Test
    public void test(){
        String str = "abcabcbb"; // abc
        String str1 = "bbbbb"; // b
        String str2 = "pwwkew"; // wke kew
        System.out.println(getLength(str));
        System.out.println(getLength(str1));
        System.out.println(getLength(str2));
    }
}
